from utils.sql_user import email_check

from utils.sql_utils.set import user_data, database, cursor


def user_login(email: str, password: str, cookie: str) -> int:
    """
    登录
    :param cookie: cookie
    :param email: 邮箱
    :param password: 密码
    :return: 0:登录成功,1:密码错误,2:邮箱不存在,3:非法请求
    """
    if email is None or password is None:
        return 3

    if email_check(email):
        return 2

    database.connect()
    cursor.execute(f'SELECT password FROM {user_data} WHERE uemail = "{email}"')
    data = cursor.fetchall()

    if data[0][0] == password:
        cursor.execute(f'update {user_data} set cookie = "{cookie}" where uemail = "{email}";')
        database.commit()
        return 0
    return 1


if __name__ == '__main__':
    print(user_login("3488636116@qq.com", "3I48l7ay", ""))
